//详情页面显示
function getQueryString(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var result = window.location.search.substr(1).match(reg);
    return result ? decodeURIComponent(result[2]) : null;
}

// function loginSuccess() {
//     var user = $("#username").val();
//     if (user == null || user == "" || user.length == 0) {
//
//     } else {
//         str = `<p id="userhover" style="margin-left: 50px">欢迎，` + user + `</p>`;
//         $("#loginuser").html(str);
//     }
// }

$(document).ready(function () {
    var userName = $("#username").val();
    //登录成功后
    // loginSuccess();
    // if (userName != null || userName !== "") {
    //     // 获取购物车数量
    //     $.ajax({
    //         url: "/getShopcartNum",
    //         data: {userName: userName},
    //         type: "post",
    //         success: function (res) {
    //             if (res.code === 0) {
    //                 $("#cart").attr("data-totalitems", res.data);
    //             }
    //         }
    //     });
    // }
    var id = getQueryString("id")
//获取图片
    $.ajax({
        url: "/detail/getImage?id=" + id,
        type: "get",
        dataType: "json",
        async: false,
        success: function (res) {
            if (res.code === 0) {
                var imgList = res.data;
                str = ``;
                for (var i = 0; i < imgList.length; i++) {
                    str += `<img src="` + imgList[i].imgAdd + `" width="420" height="500" />`;
                }
                $("#showbox").html(str);
            }
        }
    });
//中间详情部分
    $.ajax({
        url: "/detail/getDetail?id=" + id,
        type: "get",
        dataType: "json",
        async: false,
        success: function (res) {
            if (res.code === 0) {
                var product = res.data;
                var str = "";
                str += `<input id = "productId" type="hidden" value="${product.id}">
                        <input id = "shopId" type="hidden" value="${product.shopId}">
                <p class="imgname" style="margin-left: 10px;">${product.name}</p>
                <p style="margin-left: 10px;">更新时间：${product.updateTimeString}</p>
                <p class="Aprice" style="margin-left: 10px;">价格：<samp style="font-size:15px;" >￥${product.oldPrice}</samp></p>
                <p class="price"  style="margin-left: 10px;">促销价：<samp style="font-size:25px;" id="newPrice">￥${product.newPrice}</samp></p>
                <p class="price" style="margin-left: 10px;">起批量：<span>￥${product.startWholesale}</span></p>
                <div class="clear"></div>
                <p class="" style="padding-top: 10px;margin-left: 4px">发货地址: ${product.sendAddress}</p>
                <div class="clear"></div>
                <div class="clear"></div>
                <p class="chima">商家: <span id="shopUserName">${product.shopName}</span></p>
                <div class="clear"></div>
                <div class="clear"></div>
                <p class="chima">规格选择:</p>
                <div class="divtest">
                    <input type="text" style="background-color: rgb(243, 245, 238);" name="makeupCo" id="makeupCo"
                           class="makeinp" onfocus='setfocus(this,"#typenum","#makeupCo","#search-button")'
                           oninput='setinput(this,"#typenum");' placeholder="请选择需要的规格">
                    <div id="search-button"
                         onclick='setfocus(document.getElementById("makeupCo"),"#typenum","#makeupCo","#search-button")'>
                        <span class="search-img" ></span></div>
                    <select name="makeupCoSe" id="typenum" onchange='changeF(this,"#typenum","makeupCo","#search-button")'
                            size="10" style="display:none;width: 200px;z-index: 999;">                
<!--                     <option>张家辉回应渣渣辉</option>-->
<!--                     <option>赵文君</option>-->
                    </select>
                </div>
                <div class="clear"></div>
                <p class="chima">请选择数量:</p>
                <!-- 加减器 -->
                <input type="text" class="spinnerExample"/>
                <script type="text/javascript">
                    $('.spinnerExample').spinner({});
                </script>
                <div class="page-wrapper">
                    <p class="buy"><a id="firstbuy">立即购买</a>
                        <button id="addtocart" style="width: 200px; height: 50px;margin-left: 10px;">
                            <i>添加购物车</i>
                            <span class="cart-item"></span>
                        </button>
                    </p>
                </div>
                <div class="clear"></div>
                <p class="fuwu">服务承诺：</p>
                <p class="pay">支付方式：</p>`;
                $(".centerbox").html(str);
                $.ajax({
                    url: "/detail/getSpec?id=" + id,
                    type: "get",
                    dataType: "json",
                    async: false,
                    success: function (res) {
                        if (res.code === 0) {
                            var list = res.data;
                            var str = "";
                            str += `<option selected>`+ list[0].specification +`</option>`;
                            if (list.length > 1) {
                                for (var i = 1; i < list.length; i++) {
                                    str += `
                                <option>`+ list[i].specification+`</option>
                                `;
                                }
                                $("#typenum").html(str);
                            }
                            $("#typenum").html(str);
                            //设定默认值
                            $("#makeupCo").val($("#typenum option:first").val());
                        }
                    }
                });
                //加入购物车
                $('#addtocart').click(function () {
                    var button = $(this);
                    var cart = $('#cart');
                    var cartTotal = cart.attr('data-totalitems');
                    var newCartTotal = parseInt(cartTotal) + 1;
                    button.addClass('sendtocart');
                    setTimeout(function () {
                        button.removeClass('sendtocart');
                        cart.addClass('shake').attr('data-totalitems', newCartTotal);
                        setTimeout(function () {
                            cart.removeClass('shake');
                        }, 500)
                    }, 1000);

                    var value = $("#makeupCo").val();
                    if (value.length === 0) {
                        // $("#password2").focus();
                        layer.alert("请至少选择一项规格！", {
                            icon: 2,
                            skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                        });
                    } else if (userName == null || userName === "") {
                        // $("#password2").focus();
                        layer.alert("请先登录！", {
                            icon: 2,
                            skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                        });
                    } else {
                        $.ajax({
                            url: "/detail/checkPro?id=" + id + "&userName=" + userName,
                            type: "get",
                            dataType: "json",
                            // async: false,
                            success: function (res) {
                                if (res.code === 0 && res.data != null) {
                                    // $("#password2").focus();
                                    layer.alert("请勿重复添加！", {
                                        time: 1000,
                                        icon: 2,
                                        skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                                    });
                                } else {
                                    //获取数据加入购物车
                                    var productId = id;
                                    var productImg = $("#showbox img:first").attr("src");
                                    var productName = $(".imgname").text().trim();
                                    var name = $("#shopUserName").text();
                                    var price = $("#newPrice").text().substring(1);
                                    // var sumPrice = sum ;后台计算
                                    var buySum = $(".spinnerExample").val();
                                    var specification = $("#makeupCo").val();
                                    var shopId = $("#shopId").val();
                                    // var userName = userName;
                                    var commitData = {
                                        "productId": productId, "productImg": productImg, "productName": productName,
                                        "name": name, "price": price, "buySum": buySum, "specification": specification,
                                        "shopId": shopId, "userid": userName
                                    };
                                    console.log(commitData);
                                    $.ajax({
                                        url: "/detail/commitCart",
                                        type: "post",
                                        dataType: "json",
                                        data: JSON.stringify(commitData),
                                        contentType: "application/json;charset=utf-8",
                                        async: false,
                                        success: function () {
                                            if (res.code === 0) {
                                                layer.alert("成功加入购物车！", {
                                                    time: 2000,
                                                    icon: 1,
                                                    skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                                                });
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                });
                $("#firstbuy").click(function () {
                    var proId = id;
                    var buySum = $(".spinnerExample").val();
                    window.location.href = "/detailCommit.html?id=" + proId + "&buySum=" + buySum;
                    // var ids = [];
                    // var ides = ids.push(id);
                    // var productId = id;
                    // var productImg = $("#showbox img:first").attr("src");
                    // var productName = $(".imgname").text();
                    // var name = $("#userName").text();
                    // var price = $("#newPrice").text();
                    // // var sumPrice = sum ;后台计算
                    // var buySum = $(".spinnerExample").val();
                    // var specification = $("#makeupCo").val();
                    // var shopId = $("#shopId").val();
                    // var userName = userName;
                    // var commitData = {
                    //     "productId": productId, "productImg": productImg, "productName": productName,
                    //     "name": name, "price": price, "buySum": buySum, "specification": specification,
                    //     "shopId": shopId, "userName": userName
                    // };
                    // $.ajax({
                    //     url: "/deatail/saveTempPro",
                    //     type: "post",
                    //     dataType: "json",
                    //     data: JSON.stringify(commitData),
                    //     async: false,
                    //     success: function () {
                    //         layer.alert("提交成功", {
                    //             icon: 1,
                    //             skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                    //         });
                    //         $
                    //     }
                    // });
                });
            }
        }
    });

//右边部分
    $.ajax({
        url: "/detail/getShopHot?id=" + id,
        type: "get",
        dataType: "json",
        success: function (res) {
            if (res.code === 0) {
                var list = res.data;
                str = `<p class="name">——热卖商品</p>`;
                for (var i = 0; i < list.length; i++) {
                    str += `<a target="_self" href="detail.html?id=`+list[i].id+`">
                            <input type="hidden" value="${list[i].id}">
                            <img src="` + list[i].productImg + `" width="130" height="180"></a>` +
                        `<p>￥` + list[i].newPrice + `元</p>`;
                }
                $(".rightbox").html(str);
            }
        }
    });

});

